package io.xiaojl.xj2ee.task.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**  
 * <p>Title: PageWrapper</p>  
 *
 * <p>Description: 分页包装类</p>  
 *
 * @author jilong.xiao  
 * @date 2018年9月21日  
 */
public class PageWrapper<T> implements Serializable {
	/** serialVersionUID*/
	private static final long serialVersionUID = 1L;
	//总记录数
	private long totalCount;
	//每页记录数
	private int pageSize = 10;
	//总页数
	private int totalPage;
	//当前页数
	private int currPage = 1;
	//列表数据
	private List<?> list;
	//排序字段：String orderBy = 排序字段 + " desc";//按照排序字段 倒序 排序
	private String orderBy = null;
	//查询条件
	private Map<String,Object> condition = new HashMap<String,Object>();
	
	public PageWrapper(Map<String,Object> params){
		
		Iterator<String> it = params.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			if(params.get(key)!=null && !"".equals(params.get(key))){
				condition.put(key, params.get(key));
			}
		}
		
		if(params.get("page")!=null){
			currPage = Integer.parseInt((String)params.get("page"));
		}
		if(params.get("limit") != null){
			pageSize = Integer.parseInt((String)params.get("limit"));
        }
		
		//防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = SQLFilter.sqlInject((String)params.get("sidx"));
        String order = SQLFilter.sqlInject((String)params.get("order"));
        if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){
        	if("ASC".equalsIgnoreCase(order)){
        		orderBy = sidx + " ASC " ;
        	}else{
        		orderBy = sidx + " DESC ";
        	}
        }
	}

	public long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public List<?> getList() {
		return list;
	}

	public void setList(List<?> list) {
		this.list = list;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public Map<String, Object> getCondition() {
		return condition;
	}

	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}
	
}
